package mysql.swing.JFrame;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import mysql.swing.MyConnect;
import mysql.swing.Student;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MyFrame extends JFrame {

	private JPanel contentPane;
	private JTable table;
	MyConnect myconnect = new MyConnect();
	public static int k = 1;
	public static int row;
	public static String id, name, point;

	/**
	 * Create the frame.
	 */
	public MyFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setLocationRelativeTo(null);

		// connecto to database
		myconnect.connect();

		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);

		JLabel lblConnectionDatabase = new JLabel("Connection Database");
		panel.add(lblConnectionDatabase);

		JPanel panel_2 = new JPanel();
		contentPane.add(panel_2, BorderLayout.SOUTH);

		JButton btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				k = 1;
				if (getStudentSelect()) {
					EditFrame editFrame = new EditFrame();
					editFrame.setVisible(true);
				}
			}
		});

		JButton btnInsert = new JButton("Insert");
		btnInsert.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				k = 2;
				EditFrame editFrame = new EditFrame();
				editFrame.setVisible(true);
			}
		});
		panel_2.add(btnInsert);
		panel_2.add(btnUpdate);

		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				delete();
			}
		});
		panel_2.add(btnDelete);
		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loadData();
			}
		});
		panel_2.add(btnRefresh);

		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);

		table = new JTable();
		scrollPane.setViewportView(table);

	}

	public void loadData() {
		DefaultTableModel model = new DefaultTableModel();

		ResultSet rs = myconnect.getData();
		try {
			ResultSetMetaData rsMD = rs.getMetaData();
			int colNumber = rsMD.getColumnCount();
			String[] arr = new String[colNumber];
			for (int i = 0; i < colNumber; i++) {
				arr[i] = rsMD.getColumnName(i + 1);
			}

			model.setColumnIdentifiers(arr);

			while (rs.next()) {
				for (int i = 0; i < colNumber; i++) {
					arr[i] = rs.getString(i + 1);
				}
				model.addRow(arr);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		table.setModel(model);
	}

	public void delete() {
		row = table.getSelectedRow();
		if (row < 0) {
			JOptionPane.showMessageDialog(null, "Please choose one row");
			return;
		}
		int select = JOptionPane.showOptionDialog(null,
				"Are you sure delete ?", "Delete", 0,
				JOptionPane.YES_NO_OPTION, null, null, 1);
		if (select == 0) {
			myconnect.deleteId((String) table.getValueAt(row, 0));
			loadData();
		}
	}

	public boolean getStudentSelect() {
		row = table.getSelectedRow();
		if (row < 0) {
			JOptionPane.showMessageDialog(null, "Please choose one row");
			return false;
		} else {
			id = (String) table.getValueAt(row, 0);
			name = (String) table.getValueAt(row, 1);
			point = (String) table.getValueAt(row, 2);
		}
		return true;
	}

	public static void main(String[] args) {
		MyFrame myFrame = new MyFrame();
		myFrame.setVisible(true);
		myFrame.loadData();
	}

}
